﻿using System.Collections.Generic;
using UnityEngine;
using Yoozoo.Gameplay.Liberty.AI;

namespace Yoozoo.Gameplay.Liberty.World
{
    public class NpcPathState : MonoBehaviour
    {
        public int pathId;

        public PathData pathData;
        
        // 该路径所存在的npc
        public List<int> npcIdList;

        // 获取当前路径的Npc数量
        public int GetNpcCount()
        {
            if (npcIdList!=null)
            {
                return npcIdList.Count;
            }
            return 0;
        }

        public int GetDirNpcCount(PathDirection pathDirection)
        {
            int c = 0;
            if (npcIdList != null)
            {
                for (int i = 0; i < npcIdList.Count; i++)
                {
                    AIEntity otherEntity = LibertyAIUtils.GetNpc(npcIdList[i]);
                    if (otherEntity!=null)
                    {
                        if (otherEntity.data.pathDirection == pathDirection)
                        {
                            c++;
                        }
                    }
                }
            }

            return c;
        }

        /// <summary>
        /// npc行进的前方是否拥挤
        /// </summary>
        /// <param name="npcId"></param>
        /// <param name="distance"></param>
        /// <param name="number"></param>
        /// <returns></returns>
        public bool FrontCrowd(int npcId, float distance = 12f,int number = 3)
        {
            AIEntity entity = LibertyAIUtils.GetNpc(npcId);
            if (!entity)
            {
                return false;
            }
            
            if (npcIdList!=null && npcIdList.Count > number)
            {
                int count = 0;
                for (int i = 0; i < npcIdList.Count; i++)
                {
                    if (npcId != npcIdList[i])
                    {
                        AIEntity otherEntity = LibertyAIUtils.GetNpc(npcIdList[i]);

                        if (otherEntity!=null)
                        {
                            Vector3 v = otherEntity.data.position - entity.data.position;
                            if (v.magnitude < distance && Vector3.Angle(v,entity.data.forward) < 90)
                            {
                                count++;
                            }
                        }
                        
                    }
                }

                if (count >= number)
                {
                    return true;
                }
            }

            return false;
        }


        /// <summary>
        /// npc行进的方向前方是否有其它npc
        /// </summary>
        /// <param name="npcId"></param>
        /// <param name="distance"></param>
        /// <returns></returns>
        public bool FrontHasNpc(int npcId, float distance = 5f)
        {
            AIEntity entity = LibertyAIUtils.GetNpc(npcId);
            if (!entity)
            {
                return false;
            }

            if (npcIdList!=null)
            {
                for (int i = 0; i < npcIdList.Count; i++)
                {
                    if (npcId != npcIdList[i])
                    {
                        AIEntity otherEntity = LibertyAIUtils.GetNpc(npcIdList[i]);

                        if (otherEntity!=null && otherEntity.data.pathDirection == entity.data.pathDirection)
                        {
                            Vector3 v = otherEntity.data.position - entity.data.position;
                            if (v.magnitude < distance && Vector3.Angle(entity.data.forward,v) < 60)
                            {
                                return true;
                            }
                        }
                        
                    }
                }
            }
            
            return false;
        }

    }
}